/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.zimbra.common.localconfig.DebugConfig;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.LogFactory;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.sql.Timestamp;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class DbOutOfOffice
/*     */ {
/*  41 */   public static String TABLE_NAME = "out_of_office";
/*  42 */   private static Log mLog = LogFactory.getLog(DbOutOfOffice.class);
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static boolean alreadySent(DbPool.DbConnection conn, Mailbox mbox, String sentTo, long cacheDurationMillis)
/*     */     throws ServiceException
/*     */   {
/*  56 */     sentTo = sentTo.toLowerCase();
/*  57 */     boolean result = false;
/*  58 */     Timestamp cutoff = new Timestamp(System.currentTimeMillis() - cacheDurationMillis);
/*     */     
/*  60 */     PreparedStatement stmt = null;
/*  61 */     ResultSet rs = null;
/*     */     try {
/*  63 */       stmt = conn.prepareStatement("SELECT COUNT(*) FROM " + DbMailbox.qualifyZimbraTableName(mbox, TABLE_NAME) + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "sent_to = ? AND sent_on > ?");
/*     */       
/*  65 */       int pos = 1;
/*  66 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/*  67 */       stmt.setString(pos++, sentTo);
/*  68 */       stmt.setTimestamp(pos++, cutoff);
/*     */       
/*  70 */       rs = stmt.executeQuery();
/*  71 */       rs.next();
/*  72 */       int count = rs.getInt(1);
/*  73 */       result = count > 0;
/*     */     } catch (SQLException e) {
/*  75 */       throw ServiceException.FAILURE("DbOutOfOffice.getSentTime: sql exception (mailbox_id=" + mbox.getId() + " sent_to=" + sentTo + ")", e);
/*     */     }
/*     */     finally {
/*  78 */       DbPool.closeResults(rs);
/*  79 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */     
/*  82 */     if (mLog.isDebugEnabled()) {
/*  83 */       mLog.debug("DbOutOfOffice.alreadySent() returning " + result + ".  mailbox_id=" + mbox.getId() + ", sent_to='" + sentTo + "' cutoff=" + cutoff.getTime());
/*     */     }
/*     */     
/*  86 */     return result;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void setSentTime(DbPool.DbConnection conn, Mailbox mbox, String sentTo)
/*     */     throws ServiceException
/*     */   {
/* 100 */     setSentTime(conn, mbox, sentTo, System.currentTimeMillis());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void setSentTime(DbPool.DbConnection conn, Mailbox mbox, String sentTo, long sentOn)
/*     */     throws ServiceException
/*     */   {
/* 114 */     Timestamp ts = new Timestamp(sentOn);
/*     */     
/* 116 */     PreparedStatement stmt = null;
/*     */     try {
/* 118 */       String command = Db.supports(Db.Capability.REPLACE_INTO) ? "REPLACE" : "INSERT";
/* 119 */       stmt = conn.prepareStatement(command + " INTO " + DbMailbox.qualifyZimbraTableName(mbox, TABLE_NAME) + "(" + DbMailItem.MAILBOX_ID + "sent_to, sent_on)" + " VALUES (" + DbMailItem.MAILBOX_ID_VALUE + " ?, ?) ");
/*     */       
/*     */ 
/* 122 */       int pos = 1;
/* 123 */       pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 124 */       stmt.setString(pos++, sentTo.toLowerCase());
/* 125 */       stmt.setTimestamp(pos++, ts);
/* 126 */       int num = stmt.executeUpdate();
/* 127 */       if (num > 0) {
/* 128 */         if (mLog.isDebugEnabled()) {
/* 129 */           mLog.debug("DbOutOfOffice.setSentTime: ok (mailbox_id=" + mbox.getId() + " sent_to=" + sentTo + " sent_on=" + sentOn + " rows=" + num + ")");
/*     */         }
/*     */       }
/*     */       else {
/* 133 */         mLog.error("DbOutOfOffice.setSentTime: no rows updated (mailbox_id=" + mbox.getId() + " sent_to=" + sentTo + " sent_on=" + sentOn + " rows=" + num + ")");
/*     */       }
/*     */     }
/*     */     catch (SQLException e) {
/* 137 */       if (Db.errorMatches(e, Db.Error.DUPLICATE_ROW)) {
/*     */         try {
/* 139 */           stmt.close();
/*     */           
/* 141 */           stmt = conn.prepareStatement("UPDATE " + DbMailbox.qualifyZimbraTableName(mbox, TABLE_NAME) + " SET sent_on = ?" + " WHERE " + DbMailItem.IN_THIS_MAILBOX_AND + "sent_to = ?");
/*     */           
/*     */ 
/* 144 */           int pos = 1;
/* 145 */           stmt.setTimestamp(pos++, ts);
/* 146 */           pos = DbMailItem.setMailboxId(stmt, mbox, pos);
/* 147 */           stmt.setString(pos++, sentTo.toLowerCase());
/* 148 */           stmt.executeUpdate();
/*     */         } catch (SQLException nested) {
/* 150 */           throw ServiceException.FAILURE("DbOutOfOffice.setSentTime: sql exception (mailbox_id=" + mbox.getId() + " sent_to" + sentTo + " sent_on=" + sentOn + ")", nested);
/*     */         }
/*     */         
/*     */       } else {
/* 154 */         throw ServiceException.FAILURE("DbOutOfOffice.setSentTime: sql exception (mailbox_id=" + mbox.getId() + " sent_to" + sentTo + " sent_on=" + sentOn + ")", e);
/*     */       }
/*     */     }
/*     */     finally {
/* 158 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void clear(DbPool.DbConnection conn, Mailbox mbox)
/*     */     throws ServiceException
/*     */   {
/* 171 */     PreparedStatement stmt = null;
/* 172 */     ResultSet rs = null;
/*     */     try {
/* 174 */       stmt = conn.prepareStatement("DELETE FROM " + DbMailbox.qualifyZimbraTableName(mbox, TABLE_NAME) + (DebugConfig.disableMailboxGroups ? "" : " WHERE mailbox_id = ?"));
/*     */       
/* 176 */       DbMailItem.setMailboxId(stmt, mbox, 1);
/* 177 */       int num = stmt.executeUpdate();
/*     */       
/* 179 */       mLog.debug("DbOutOfOffice.clear() mbox=" + mbox.getId() + " rows=" + num);
/*     */     } catch (SQLException e) {
/* 181 */       throw ServiceException.FAILURE("DbOutOfOffice.clear acctId=" + mbox.getAccountId(), e);
/*     */     } finally {
/* 183 */       DbPool.closeResults(rs);
/* 184 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */   
/*     */   public static void prune(DbPool.DbConnection conn, long cacheDurationMillis)
/*     */     throws ServiceException
/*     */   {
/* 191 */     if (DebugConfig.disableMailboxGroups) {
/* 192 */       return;
/*     */     }
/* 194 */     PreparedStatement stmt = null;
/*     */     try {
/* 196 */       Timestamp cutoff = new Timestamp(System.currentTimeMillis() - cacheDurationMillis);
/* 197 */       stmt = conn.prepareStatement("DELETE FROM " + TABLE_NAME + " WHERE sent_on <= ?");
/* 198 */       stmt.setTimestamp(1, cutoff);
/* 199 */       int num = stmt.executeUpdate();
/*     */       
/* 201 */       if (mLog.isDebugEnabled())
/* 202 */         mLog.debug("DbOutOfOffice.prune() deleted " + num + " rows with cutoff <= " + cutoff.getTime());
/*     */     } catch (SQLException e) {
/* 204 */       throw ServiceException.FAILURE("DbOutOfOffice.prune()", e);
/*     */     } finally {
/* 206 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/DbOutOfOffice.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */